package com.futureCloudMOM.core.service.impl;

import com.futureCloudMOM.common.exception.base.BaseException;
import com.futureCloudMOM.core.base.service.impl.BaseServiceImpl;
import com.futureCloudMOM.core.domain.WmsErpSourceSub;
import com.futureCloudMOM.core.mapper.WmsErpSourceMainMapper;
import com.futureCloudMOM.core.mapper.WmsErpSourceSubMapper;
import com.futureCloudMOM.core.service.WmsErpSourceSubService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
* 【WMS_ERP_SourceSub】的数据库操作Service实现
*
* @author Administrator
* @since 2023-06-07 09:14:43
*/
@AllArgsConstructor
@Service
public class WmsErpSourceSubServiceImpl extends BaseServiceImpl<WmsErpSourceSubMapper, WmsErpSourceSub> implements WmsErpSourceSubService{

    // 源单主表
    private final WmsErpSourceMainMapper mainMapper;
    // 源单子表
    private final WmsErpSourceSubMapper subMapper;

    @Override
    public void deductQty(Long ERPMainId, Long ERPSubId, BigDecimal qty) {
        // 更新源单主表 待定

        // 更新源单子表
        WmsErpSourceSub wmsErpSourceSub = subMapper.selectById(ERPSubId);
        // 判断数量是否超出未关联数量
        if(qty.compareTo(wmsErpSourceSub.getNotRelationQty()) > 0){
            throw new BaseException("数量超出未关联数量,请核对");
        }
        // 已关联数量增加
        wmsErpSourceSub.setRelationQty(wmsErpSourceSub.getRelationQty().add(qty));
        // 未关联数量扣除
        wmsErpSourceSub.setNotRelationQty(wmsErpSourceSub.getNotRelationQty().subtract(qty));
        subMapper.updateById(wmsErpSourceSub);
    }

    @Override
    public void backQty(Long ERPMainId, Long ERPSubId, BigDecimal qty) {
        // 更新源单主表 待定

        // 更新源单子表
        WmsErpSourceSub wmsErpSourceSub = subMapper.selectById(ERPSubId);
        // 已关联数量扣除
        wmsErpSourceSub.setRelationQty(wmsErpSourceSub.getRelationQty().subtract(qty));
        // 未关联数量增加
        wmsErpSourceSub.setNotRelationQty(wmsErpSourceSub.getNotRelationQty().add(qty));
        subMapper.updateById(wmsErpSourceSub);
    }


}
